<!--
 * @Description: 库存销量组件
 * @Author: 梁萌
 * @Date: 2020-12-12 19:47:14
 * @LastEditTime: 2020-12-19 16:43:11
 * @FilePath: \vision\src\components\Stock.vue
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      chartIntance: null,
      allData: null, //服务器返回的数据
      currentIndex: 0, //当前显示的页码数
      timeId: null, //定时器
    };
  },
  created() {
    //组件创建完成后进行回调函数的注册
    this.$socket.registerCallBack("stockData", this.getData);
  },
  mounted() {
    this.initChart();
    //this.getData();
    //发送数据给服务器,获取数据
    this.$socket.send({
      action: "getData",
      socketType: "stockData",
      chartName: "stock",
      value: "",
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    clearInterval(this.timeId);
    //在组件销毁时进行回调函数取消
    this.$socket.unRegisterCallBack("stockData");
  },
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartIntance = this.$echarts.init(this.$refs.stock_ref, this.theme); //初始化echarts实例对象
      //对图表初始化配置的控制
      const initOption = {
        title: {
          text: "▎库存和销量分析",
          left: 20,
          top: 20,
        },
      };
      this.chartIntance.setOption(initOption);

      //鼠标移入和移出事件
      this.chartIntance.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.chartIntance.on("mouseout", () => {
        this.startInterval();
      });
    },

    //获取服务器数据
    getData(ret) {
      //const { data: ret } = await this.$http.get("stock");
      this.allData = ret;
      this.updateChart();
      this.startInterval();
    },

    //更新图表
    updateChart() {
      //圆环的中心点坐标
      const centerArr = [
        ["18%", "40%"],
        ["50%", "40%"],
        ["82%", "40%"],
        ["34%", "75%"],
        ["66%", "75%"],
      ];
      //饼图渐变颜色值
      const colorArr = [
        ["#4FF778", "#0BA82C"],
        ["#E5DD45", "#E8B11C"],
        ["#E8821C", "#E55445"],
        ["#5052EE", "#AB6EE5"],
        ["#23E5E5", "#2E72BF"],
      ];

      const start = this.currentIndex * 5;
      const end = (this.currentIndex + 1) * 5;

      const showData = this.allData.slice(start, end);
      const seriesArr = showData.map((item, index) => {
        return {
          type: "pie",
          center: centerArr[index],
          hoverAnimation: false, //关闭鼠标移入到饼图时的动画效果
          labelLine: {
            show: false, //隐藏指示线
          },
          label: {
            position: "center", //控制文字在饼图内显示
            color: colorArr[index][0],
          },
          data: [
            {
              name: item.name + "\n\n" + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  { offset: 0, color: colorArr[index][0] },
                  { offset: 1, color: colorArr[index][1] },
                ]),
              },
            },
            {
              value: item.stock,
              itemStyle: {
                color: "#333843",
              },
            },
          ],
        };
      });
      const dataOption = {
        series: seriesArr,
      };

      this.chartIntance.setOption(dataOption);
    },

    //当浏览器大小变化时进行屏幕适配
    screenAdapter() {
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6;
      const innerRadius = titleFontSize * 2.8; //内圆半径
      const outterRadius = innerRadius * 1.125; //外圆半径
      //和分辨率大小相关配置
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
        series: [
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
          {
            type: "pie",
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2,
            },
          },
        ],
      };

      this.chartIntance.setOption(adapterOption);
      this.chartIntance.resize();
    },

    //定时翻页
    startInterval() {
      if (this.timeId) {
        clearInterval(this.timeId);
      }
      //总页数
      var totalPage =
        this.allData.length % 5 == 0
          ? this.allData.length / 5
          : this.allData.length / 5 + 1;

      //定时器
      this.timeId = setInterval(() => {
        this.currentIndex++;
        if (this.currentIndex > totalPage - 1) {
          this.currentIndex = 0;
        }
        this.updateChart();
      }, 5000);
    },
  },
  computed: {
    ...mapState(["theme"]),
  },
  watch: {
    theme() {
      this.chartIntance.dispose();
      this.initChart();
      this.screenAdapter();
      this.updateChart();
    },
  },
};
</script>

<style>
</style>